<?php
// app/Controller/UsersController.php
class UsersController extends AppController {


    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('add', 'logout');
    }

    public function index()
    {
        $this->User->recursive = 0;
        $this->set('Users', $this->paginate());
    }

    public function view($id = null)
    {
        $this->User->id = $id;
        if (!$this->User->exists())
        {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    public function add()
    {
        $this->loadModel('Institution');
        $instituions_list = $this->Institution->find('list', array('fields'=>array('id', 'name')));
        $this->set('instituions_list', $instituions_list);
        
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
    }
    public function edit($id)
    {
        $this->loadModel('Institution');
        $instituions_list = $this->Institution->find('list', array('fields'=>array('id', 'name')));
        $this->set('instituions_list', $instituions_list);
        $allowed = true;
        
        if($id != $this->Auth->user('id'))
        {
            $allowed = false;
        }
        if($this->Auth->user('role') == 'Administrador')
        {
            $allowed = true;
        }
        
        if($allowed)
        {
            if($this->data)
            {
                //die(debug($this->data));
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('<div class="alert alert-success">Alterações realizadas com sucesso!</div>'));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('<div class="alert alert-error">Enviado com sucesso!</div>'));
                }
            }
            else
            {
                $this->data = $this->User->find('first', array('conditions'=>array('id'=>$id)));
            }
        }
        $this->set('allowed', $allowed);
        
        
    }

    public function admin_edit($id = null)
    {
        $this->User->id = $id;
        
        if (!$this->User->exists())
        {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put'))
        {
            if ($this->User->save($this->request->data))
            {
                $this->Session->setFlash(__('The user has been saved'));
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        }
        else
        {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

    public function admin_delete($id = null)
    {
        if (!$this->request->is('post'))
        {
            throw new MethodNotAllowedException();
        }
        $this->User->id = $id;
        if (!$this->User->exists())
        {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->delete())
        {
            $this->Session->setFlash(__('User deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
    
    
    public function login()
    {
        $this->layout = 'login';

        if ($this->Auth->login())
        {
            $this->redirect($this->Auth->redirect());
        }
        else
        {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
        
        
    }

    public function logout()
    {
        $this->redirect($this->Auth->logout());
    }
}
?>